package www.com.springboot_20200312.rabbitmqDemo.HeadersExchange.producer;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import www.com.springboot_20200312.rabbitmqDemo.HeadersExchange.message.DemoHeadersMessage;

/**
 * @program: springboot_20200312
 * @ClassName Demo01Producer
 * @description:
 * @author: jht
 * @create: 2021-12-07 10:25
 * @Version 1.0
 **/
@Component
public class DemoHeadersProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void syncSend(Integer id, String headerValue) {
        // 创建 MessageProperties 属性
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader(DemoHeadersMessage.HEADER_KEY, headerValue); // 设置 header
        // 创建 Message 消息
        DemoHeadersMessage demoHeadersMessage =  new DemoHeadersMessage();
        demoHeadersMessage.setId(id);
        Message message = rabbitTemplate.getMessageConverter().toMessage(
                demoHeadersMessage, messageProperties);
        // 同步发送消息
        rabbitTemplate.send(DemoHeadersMessage.EXCHANGE, null, message);
    }
}
